package lanou.giftsay_01.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import lanou.giftsay_01.R;
import lanou.giftsay_01.bean.FilterBean;
import lanou.giftsay_01.bean.SearchBean;
import lanou.giftsay_01.main.categorymain.SelectGiftOnClickListenner;

/**
 * Created by dllo on 16/8/24.
 */
public class PopRvAdapter extends RecyclerView.Adapter<PopRvAdapter.MyPopViewHolder> {
    private Context context ;
    private FilterBean filterBean ;
    private int tag ;
    private SelectGiftOnClickListenner selectGiftOnClickListenner ;
    private int a = 0,b = 0,c = 0,d = 0;

    public void setSelectGiftOnClickListenner(SelectGiftOnClickListenner selectGiftOnClickListenner) {
        this.selectGiftOnClickListenner = selectGiftOnClickListenner;
    }

    public void setTag(int tag) {
        this.tag = tag;
    }

    public PopRvAdapter(Context context) {
        this.context = context;
    }

    public void setFilterBean(FilterBean filterBean) {
        this.filterBean = filterBean;
        notifyDataSetChanged();
    }

    public void setPos(int pos){
        switch (tag){
            case 0:
                a = pos;
                break;
            case 1:
                b = pos;
                break;
            case 2:
                c = pos;
                break;
            case 3:
                d = pos;
                break;
        }
        notifyDataSetChanged();
    }

    @Override
    public MyPopViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_poprv,parent,false);
        MyPopViewHolder myPopViewHolder = new MyPopViewHolder(view);

        return myPopViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyPopViewHolder holder, final int position) {
        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectGiftOnClickListenner.onClick(position,filterBean,tag );
                setPos(position);
            }
        });
        if (position == 0){
            holder.tv.setText("全部");
        //    holder.tv.setBackgroundColor(0XFFFF2D47);
        }else {
            holder.tv.setText(filterBean.getData().getFilters().get(tag).getChannels().get(position-1).getName());
        }

        Log.d("PopRvAdapter", "a:" + a);
        switch (tag){
            case 0:
                if(position == a){
                    holder.tv.setBackgroundColor(0XFFFF2D47);
                } else {
                    holder.tv.setBackgroundColor(0xffffffff);
                }
                break;
            case 1:
                if(position == b){
                    holder.tv.setBackgroundColor(0XFFFF2D47);
                } else {
                    holder.tv.setBackgroundColor(0xffffffff);
                }
                break;
            case 2:
                if(position == c){
                    holder.tv.setBackgroundColor(0XFFFF2D47);
                } else {
                    holder.tv.setBackgroundColor(0xffffffff);
                }
                break;
            case 3:
                if(position == d){
                    holder.tv.setBackgroundColor(0XFFFF2D47);
                } else {
                    holder.tv.setBackgroundColor(0xffffffff);
                }
                break;
        }


    }

    @Override
    public int getItemCount() {
        return filterBean != null ? filterBean.getData().getFilters().get(tag).getChannels().size()+1 : 0 ;
    }

    public class MyPopViewHolder extends RecyclerView.ViewHolder {
        TextView tv ;
        public MyPopViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv_pop_item);
        }
    }
}
